package com.example.music.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.music.dao.SingerMapper;
import com.example.music.dto.SingerCountryDto;
import com.example.music.dto.SingerDto;
import com.example.music.dto.SingerSexDto;
import com.example.music.dto.SongListStyleDto;
import com.example.music.entity.Singer;
import com.example.music.entity.SongList;
import com.example.music.service.SingerService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ContentValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SingerServiceImpl extends BaseServiceImpl<Singer, SingerMapper> implements SingerService {
    @Autowired
    SingerMapper singerMapper;

    @Override
    protected SingerMapper getMapper() {
        return singerMapper;
    }

    @Override
    public List<Singer> findSingerByName(String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return singerMapper.selectList(queryWrapper);
    }

    @Override
    public List<Singer> findSingerBySex(String sex) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sex", sex);
        return singerMapper.selectList(queryWrapper);
    }

    @Override
    public Integer insert(Singer singer) {
        return add(singer);
    }

    @Override
    public Integer update(Singer singer) {
        return singerMapper.updateById(singer);
    }

    @Override
    public boolean delete(Integer id) {
        return deleteById(id.toString()) != 0;
    }

    @Override
    public Singer selectById(Integer id) {
        return selectItemById(id.toString());
    }

    @Override
    public List<Singer> allSinger() {
        return selectAll();
    }

    @Override
    public AjaxJson updateSingerPic(Integer id, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"img"+
                ContentValue.Slash+"singerPic";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = "/img/singerPic/"+fileName;
        try {
            file.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id.longValue());
            singer.setPic(mysqlDir);
            Integer update = update(singer);
            if (update == 1){
                return AjaxJson.getSuccess();
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxJson sex() {
        List<Singer> singers = singerMapper.selectList(null);
        Map<Integer, List<Singer>> collect = singers.parallelStream().collect(Collectors.groupingBy(Singer::getSex));
        ArrayList<SingerSexDto> arrayList = new ArrayList<>();
        collect.forEach((key,value) -> {
            if (key == 0){
                arrayList.add(new SingerSexDto("女",value.size()));
            }else if (key == 1){
                arrayList.add(new SingerSexDto("男",value.size()));
            }else if (key == 2){
                arrayList.add(new SingerSexDto("组合",value.size()));
            }else if (key == 3){
                arrayList.add(new SingerSexDto("不明",value.size()));
            }
        });
        List<SingerSexDto> collect1 = arrayList.stream().sorted(Comparator.comparing(SingerSexDto::getNum).reversed()).limit(7).collect(Collectors.toList());
        return AjaxJson.getSuccessData(collect1);
    }

    @Override
    public AjaxJson singerByCountry() {
        List<Singer> singers = singerMapper.selectList(null);
        Map<String, List<Singer>> collect = singers.parallelStream().collect(Collectors.groupingBy(Singer::getLocation));
        ArrayList<SingerCountryDto> arrayList = new ArrayList<>();
        collect.forEach((key,value) -> {
            arrayList.add(new SingerCountryDto(key,value.size()));
        });
        List<SingerCountryDto> collect1 = arrayList.stream().sorted(Comparator.comparing(SingerCountryDto::get总数).reversed()).limit(7).collect(Collectors.toList());
        return AjaxJson.getSuccessData(collect1);
    }


}
